<?php

namespace app\yuedu\controller;

use app\yuedu\validate\Yue;
use think\Controller;
use think\Db;
use think\facade\View;
use think\Request;

class Yuedu extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function add()
    {
        return $this->fetch();
    }


    public function create()
    {
        //请求的接口URL
        $apiUrl = 'http://v.juhe.cn/joke/content/list.php';

//请求参数
        $params = [
            'key' => '8050cf3d5a73fd1d28161e1cba62eee3',
            'sort' => 'desc',
            'time' => 'time()',
            'pagesize' => 3
        ];
//参数数组转换成字符串
        $paramsString = http_build_query($params);

//发起接口网络请求
        $response = null;
        try {
            $response = $this->juheHttpRequest($apiUrl, $paramsString, 1);
        } catch (Exception $e) {
            var_dump($e);
            //此处根据自己的需求进行具体的异常处理
        }
        if (!$response) {
            echo '请求异常' . PHP_EOL;
        }
//接收接口返回内容
        $result = json_decode($response, true);
        if (empty($result['result']['data'])) {
            echo "内容不能为空";
        }
            if (!$result) {
            echo '请求异常' . PHP_EOL;
        }
        $errorCode = $result['error_code'];
        if ($errorCode == 0) {
//            $validate=new Yue();
//            $result = $validate->check($result['result']['data']);
//
//            if(!$result){
//                return json(['code'=>201,'msg'=>$validate->getError()]);
//            }

            Db::name('yuedu')->insertAll($result['result']['data']);
            $this->success('添加成功','show');
            $data = $result['result'];
        } else {
            echo "请求异常：{$errorCode}_{$result['reason']}" . PHP_EOL;
        }
//打印接口返回结果
        var_dump($result);
    }
    public function show(){
        $keyword=\request()->param('keyword');
        $data=Db::name('yuedu')->where('content','like',"%$keyword%")->order('create_time','desc')->paginate([
            'list_rows'=>3,
            'query'=>\request()->param()
        ]);
        return View::fetch('show',['data'=>$data]);
    }
     public function del(){
        $id=\request()->param('id');
       $res=\app\yuedu\model\Yue::destroy($id);
        if ($res){
            return $this->success('软删除成功','show');
        }else{
            return $this->error('软删除失败','show');
        }
     }
    /**
     * 发起网络请求函数
     * @param String $url 请求的URL
     * @param bool $params  请求的参数内容
     * @param int $isPost   是否POST请求
     * @return bool|string  返回内容
     */
    public function juheHttpRequest($url, $params = false, $isPost = 0)
    {
        $httpInfo = [];
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT,  'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($ch, CURLOPT_TIMEOUT, 12);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($isPost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        $reponse = curl_exec($ch);
        if ($reponse === FALSE) {
            // echo "cURL Error: ".curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $reponse;
    }
}
